#include <pthread.h>
#include <stdio.h>

static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

void *thread_handler(void *argv)
{
    int *b = argv;
    pthread_mutex_lock(&lock);
    (*b)++;
    pthread_mutex_unlock(&lock);
}

int main(int argc, char const *argv[])
{
    int a = 0;
    pthread_t threads[20000];

    for (size_t i = 0; i < 20000; i++)
    {
        pthread_create(threads + i, NULL, thread_handler, &a);
    }

    for (size_t i = 0; i < 20000; i++)
    {
        pthread_join(threads[i], NULL);
    }

    printf("a的最终大小: %d\n", a);

    return 0;
}
